# -*- encoding: utf-8 -*-

from robot.api import logger
from robotlibcore import keyword
from robot.utils.asserts import assert_equal, assert_not_equal
import json
import os

from .JsonSchemaLib import JsonSchemaLib

class UtilLib(object):

    def __init__(self):
        self.jlib = JsonSchemaLib()

    @keyword('Set To Dictionary')
    def set_to_dictionary(self, dictionary, *key_value_pairs, **items):
        """Adds the given ``key_value_pairs`` and ``items`` to the ``dictionary``.
        | Set To Dictionary | ${D1} | key | value | second | ${2} |
        """
        if len(key_value_pairs) % 2 != 0:
            raise ValueError("Adding data to a dictionary failed. There "
                             "should be even number of key-value-pairs.")
        for i in range(0, len(key_value_pairs), 2):
            dictionary[key_value_pairs[i]] = key_value_pairs[i+1]
        dictionary.update(items)
        return dictionary

    @keyword('Append To List')
    def append_to_list(self, list_, *values):
        for value in values:
            list_.append(value)

    @keyword('Equal As Int')
    def equal_as_int(self, first, second):
        
        first = int(first)
        second = int(second)

        if first == second:
            logger.info("[校验相等] 两个字符串 %d == %d, 相等校验通过" % (first, second))
        assert_equal(first, second, msg="[校验相等] 两个字符串 %d == %d, 不相等校验不通过" % (first, second))

    @keyword('Not Equal As Int')
    def not_equal_as_int(self, first, second):

        first = int(first)
        second = int(second)

        if first != second:
            logger.info("[校验不相等] 两个整数  %d == %d, 不相等校验通过" % (first, second))
        assert_not_equal(first, second, msg="[校验不相等] 两个字符串 %d == %d, 相等校验不通过" % (first, second))

    @keyword('Equal As String')
    def equal_as_string(self, first, second):
        
        first = str(first)
        second = str(second)
        msg = first + " == " + second

        if first == second:
            logger.info(("[校验相等] 两个字符串 " + msg + "，相等校验通过"))
        assert_equal(first, second, msg=("[校验相等] 两个整数 " + msg + "，不相等校验不通过"))
    
    @keyword('Not Equal As String')
    def not_equal_as_string(self, first, second):
        
        first = str(first)
        second = str(second)
        msg = first + " != " + second

        if first == second:
            logger.info(("[校验不相等] 两个字符串 " + msg + "，不相等校验通过"))
        assert_not_equal(first, second, msg=("[校验不相等] 两个字符串 " + msg + "，相等校验不通过"))
    
    @keyword('Equal As Json String')
    def equal_as_json_string(self, first, second, sensitive=False):

        if not isinstance(first, dict):
            logger.info(1)
            first = json.loads(first)
        
        if not isinstance(second, dict):
            second = json.loads(second)
        
        if sensitive is not False:
            for fk in first.keys():
                first[fk] = str(first[fk]).upper()
            
            for sk in second.keys():
                second[sk] = str(second[sk]).upper()

        if first == second:
            logger.info("[检验Json相等] 校验通过, {} == {}".format(first, second))
        
        assert_equal(first, second, msg="[校验Json相等] 不通过".format(first, second))
    
    @keyword('Json Loads')
    def json_loads(self, data_str):

        return json.loads(data_str)
    
    @keyword('Set Int Variable')
    def set_int_variable(self, value):

        return int(value)

    @keyword('Set String Variable')
    def set_string_variable(self, value):
        return str(value)

    @keyword('Set Bool Variable')
    def set_bool_variable(self, value):
        return bool(value)

    def get_venv(self):

        return os.environ.get("ROBOT_VENV")

    @keyword('Get UUID')
    def get_uuid(self):

        return self.jlib.getFileName()